import * as vscode from 'vscode';
import { importApi } from './importApi';

export async function useApi() {
  const textEditor = vscode.window.activeTextEditor;
  if (!textEditor) {
    vscode.window.showWarningMessage("活动的文本编辑器不存在");
    return;
  }
  const document = textEditor.document;
  if (document.uri.scheme !== 'file') {
    vscode.window.showWarningMessage('当前文本编辑器的文档类型不是file');
    return;
  }
  if (!document.uri.fsPath.match(/\.(ts|tsx|js|jsx|vue)$/)) {
    vscode.window.showWarningMessage('目前只支持ts/tsx/js/jsx/vue文件');
    return;
  }

  const importResult = await importApi();
  if (!importResult) { return; }

  const { importName } = importResult;
  return await textEditor.edit((editBuilder) => {
    editBuilder.replace(textEditor.selection, importName);
  });
}